const path = require('path')
const { app, BrowserWindow, session } = require('electron')
const windowStateKeeper = require('electron-window-state')

const { capture } = require('./readItem')

const createWindow = () => {
  session.fromPartition('persist:part1')

  let mainWindowState = windowStateKeeper({
    defaultWidth: 1000,
    defaultHeight: 800
  })

  const mainWindow = new BrowserWindow({
    x: mainWindowState.x,
    y: mainWindowState.y,
    width: mainWindowState.width,
    height: mainWindowState.height,

    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      preload: path.join(__dirname, 'preload.js'),
      enableRemoteModule: true,
      partition: 'persist:part1'
    }
  })

  mainWindow.loadURL('http://localhost:8080')

  mainWindow.webContents.openDevTools()

  mainWindowState.manage(mainWindow)

  capture(mainWindow)
}

app.whenReady().then(() => {
  createWindow()
})

// Quit when all windows are closed - (Not macOS - Darwin)
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

// When app icon is clicked and app is running, (macOS) recreate the BrowserWindow
// app.on('activate', () => {
//   if (mainWindow === null) createWindow()
// })